Motivation

I found a problem with the previous code: I added info on butterfly_flight to the data before calculating the distances and that makes the paths larger than they actually are. This version does not have that problem.

I also changed the code in order to use purrr, a package for doing functional programming. Basically, it makes it way easier to maintain the code by making it shorter.

I suppose that’s enough rambling from me, let’s give you the tables, that’s what you want after all 😄

Data processing

Loads and installs packages used if needed

list_of_packages <- c("tidyverse", "here", "fs", "plotly")
new_packages <- list_of_packages[!(list_of_packages %in% installed.packages()[,"Package"])]
if(length(new_packages)) install.packages(new_packages)

library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3     ✓ purrr   0.3.4
## ✓ tibble  3.1.0     ✓ dplyr   1.0.5
## ✓ tidyr   1.1.2     ✓ stringr 1.4.0
## ✓ readr   1.4.0     ✓ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(here)
## here() starts at /home/francisko/coding/r/flight-path-experiment
library(fs)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout

Loading files

fs::dir_ls(here::here('wand'), recurse = T, regexp = "unpaired-points-xyz.csv$") %>% 
  purrr::map_dfr(readr::read_csv, .id = "source") -> all_flights
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
## 
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   x_1 = col_double(),
##   y_1 = col_double(),
##   z_1 = col_double()
## )
all_flights %>% 
  dplyr::distinct(source)
## # A tibble: 35 x 1
##    source                                                                       
##    <chr>                                                                        
##  1 /home/francisko/coding/r/flight-path-experiment/wand/b195-t1/b195-t1-unpaire…
##  2 /home/francisko/coding/r/flight-path-experiment/wand/b195-t2/b195-t2-unpaire…
##  3 /home/francisko/coding/r/flight-path-experiment/wand/b195-t3/b195-t3-unpaire…
##  4 /home/francisko/coding/r/flight-path-experiment/wand/b195-t4/b195-t4-unpaire…
##  5 /home/francisko/coding/r/flight-path-experiment/wand/b195-t5/b195-t5-unpaire…
##  6 /home/francisko/coding/r/flight-path-experiment/wand/b206-t1/b206-t1-unpaire…
##  7 /home/francisko/coding/r/flight-path-experiment/wand/b206-t2/b206-t2-unpaire…
##  8 /home/francisko/coding/r/flight-path-experiment/wand/b206-t3/b206-t3-unpaire…
##  9 /home/francisko/coding/r/flight-path-experiment/wand/b206-t4/b206-t4-unpaire…
## 10 /home/francisko/coding/r/flight-path-experiment/wand/b211-t1/b211-t1-unpaire…
## # … with 25 more rows

Defining functions

calc_flight_dist <- function(dat) {
  dat_dist <-  {{dat}} %>% 
  dplyr::filter(!is.na(x_1)) %>%  # removes NA
  dist() %>%  # calculates distance
  as.matrix()  # converts it to a matrix

return(sum((dat_dist[row(dat_dist) == col(dat_dist) + 1])))  # sums the distance between points by summing the points in line n and column n + 1
  
}
plots_flight_path <- function(dat) {
  
plotly::plot_ly(data = dat,
        type = "scatter3d", 
        x = dat$x_1 ,
        y = dat$y_1,
        z = dat$z_1,
        mode = "lines")
}
calc_straight_dist <- function(dat) {
  
dist_dat <- 
  dat %>% 
  dplyr::filter(!is.na(x_1)) %>%
  dist() %>% 
  as.matrix()

return(dist_dat[1, nrow(dist_dat)])  
}
all_flights %>%
  dplyr::group_by(source) %>%
  tidyr::nest() %>% 
  dplyr::mutate(distance = purrr::map_dbl(data, ~ calc_flight_dist(.x)),
                straight_distance = purrr::map_dbl(data, ~ calc_straight_dist(.x)),
                sinuosity = distance / straight_distance,
                plot_3d = purrr::map(data, ~ plots_flight_path(.x)),
                butterfly = stringr::str_extract(source, pattern = "b\\d{3}"),
                take = stringr::str_extract(source, pattern = "t\\d{1}")) -> processed

Here’s the table

processed %>% 
  dplyr::select(-1, -data, -plot_3d) %>% 
  DT::datatable(extensions = 'Buttons',
                options = list(dom = 'Blfrtip',
                               buttons = c('copy', 'csv', 'excel', 'pdf', 'print'),
                               lengthMenu = list(c(10,25,50,-1),
                                                 c(10,25,50,"All"))))
## Adding missing grouping variables: `source`